﻿'
' DKP for DotNetNuke® - http://dkpdnn.codeplex.com
' Copyright (c) 2008
' by Casey Lengacher ( http://dkpdnn.codeplex.com )
'
' DotNetNuke®, DNN®, and the DotNetNuke logo are trademarks of DotNetNuke Corporation.
'
' Permission is hereby granted, free of charge, to any person obtaining a copy
' of this software and associated documentation files (the "Software"), to deal
' in the Software without restriction, including without limitation the rights
' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the Software is
' furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in
' all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
' SOFTWARE.
'

Imports System.Net
Imports DotNetNuke.Common.Utilities

Namespace DotNetNuke.Modules.tss_DKP

    Partial Public Class tss_ucMobs

        Inherits Entities.Modules.PortalModuleBase

#Region "Private Variables"

        Private Enum PageState As Integer
            NotSet = -1
            View = 0
            Add = 1
            SingleUpdate = 2
            MultiUpdate = 3
        End Enum

#End Region

#Region "Private Properties"

        Private Property vsPageSize() As Integer
            Get
                If ViewState("tss_MobPageSize") Is Nothing Then
                    If (Request.Cookies.Get("tss_MobPageSize") Is Nothing) Then
                        Return ddlPageSize.Items(0).Value
                    Else
                        Return CInt(Request.Cookies.Get("tss_MobPageSize").Value)
                    End If
                Else
                    Return CInt(ViewState("tss_MobPageSize"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_MobPageSize") = Value
                Response.Cookies("tss_MobPageSize").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_MobPageSize").Value = Value
            End Set
        End Property

        Private Property vsSortExpression() As String
            Get
                If ViewState("tss_MobSortExpression") Is Nothing Then
                    If (Request.Cookies.Get("tss_MobSortExpression") Is Nothing) Then
                        Return "MobName"
                    Else
                        Return CStr(Request.Cookies.Get("tss_MobSortExpression").Value)
                    End If
                Else
                    Return CStr(ViewState("tss_MobSortExpression"))
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("tss_MobSortExpression") = Value
                Response.Cookies("tss_MobSortExpression").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_MobSortExpression").Value = Value
            End Set
        End Property

        Private Property vsSortDirection() As String
            Get
                If ViewState("tss_MobSortDirection") Is Nothing Then
                    If (Request.Cookies.Get("tss_MobSortDirection") Is Nothing) Then
                        Return "ASC"
                    Else
                        Return CStr(Request.Cookies.Get("tss_MobSortDirection").Value)
                    End If
                Else
                    Return CStr(ViewState("tss_MobSortDirection"))
                End If
            End Get
            Set(ByVal Value As String)
                ViewState("tss_MobSortDirection") = Value
                Response.Cookies("tss_MobSortDirection").Expires = DateTime.Now.AddYears(10)
                Response.Cookies("tss_MobSortDirection").Value = Value
            End Set
        End Property

        Private Property vsCurrentPage() As Integer
            Get
                If ViewState("tss_MobCurrentPage") Is Nothing Then
                    Return 1
                Else
                    Return CInt(ViewState("tss_MobCurrentPage"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_MobCurrentPage") = Value
            End Set
        End Property

        Private Property vsTotalPages() As Integer
            Get
                If ViewState("tss_MobTotalPages") Is Nothing Then
                    Return 1
                Else
                    Return CInt(ViewState("tss_MobTotalPages"))
                End If
            End Get
            Set(ByVal Value As Integer)
                ViewState("tss_MobTotalPages") = Value
            End Set
        End Property

        Private Property vsPageState() As PageState
            Get
                If ViewState("tss_MobPageState") Is Nothing Then
                    Return PageState.NotSet
                Else
                    Return CStr(ViewState("tss_MobPageState"))
                End If
            End Get
            Set(ByVal Value As PageState)
                ViewState("tss_MobPageState") = Value
            End Set
        End Property

#End Region

#Region "Private Methods"

        Private Sub BindPage()

            Dim tss_DKP As New tss_DataController
            Dim intRowsAffected As Integer
            Dim intPageNumber As Integer = vsCurrentPage
            Dim dblPagesAffected As Double = 1

            ddlPageSize.SelectedValue = ddlPageSize.Items.FindByText(vsPageSize).Value

            grdMobs.PageSize = vsPageSize

            For Each currentColumn As DataGridColumn In grdMobs.Columns
                currentColumn.HeaderText = currentColumn.HeaderText.Substring(0, _
                    IIf(currentColumn.HeaderText.IndexOf(" <img") > 0, currentColumn.HeaderText.IndexOf(" <img"), currentColumn.HeaderText.Length))
                If (currentColumn.SortExpression = vsSortExpression) Then
                    If (vsSortDirection = "ASC") Then
                        currentColumn.HeaderText = currentColumn.HeaderText _
                            + " <img src='/DesktopModules/tss_DKP/Images/grid_icon_uparrow.png' border=0 height=10 width=10>"
                    Else
                        currentColumn.HeaderText = currentColumn.HeaderText _
                            + " <img src='/DesktopModules/tss_DKP/Images/grid_icon_downarrow.png' border=0 height=10 width=10>"
                    End If
                End If
            Next

            grdMobs.DataSource = tss_DKP.tss_GetMobsByPage(CInt(Settings("BoardID")), intPageNumber, grdMobs.PageSize, vsSortExpression, vsSortDirection, intRowsAffected)
            grdMobs.DataBind()

            If (grdMobs.Items.Count = 0) And (intPageNumber > 1) Then
                intPageNumber = intPageNumber - 1
                vsCurrentPage = intPageNumber.ToString()
                grdMobs.DataSource = tss_DKP.tss_GetMobsByPage(CInt(Settings("BoardID")), intPageNumber, grdMobs.PageSize, vsSortExpression, vsSortDirection, intRowsAffected)
                grdMobs.DataBind()
            End If

            dblPagesAffected = System.Math.Ceiling(intRowsAffected / grdMobs.PageSize)
            If (dblPagesAffected < 1) Then
                dblPagesAffected = 1
            End If
            vsTotalPages = dblPagesAffected.ToString()
            lblCurrentPage.Text = vsCurrentPage
            lblTotalPages.Text = vsTotalPages

            If (intPageNumber = 1) Then
                lnkPreviousPage.Enabled = False

                If (dblPagesAffected > 1) Then
                    lnkNextPage.Enabled = True
                    lnkLastPage.Enabled = True
                Else
                    lnkNextPage.Enabled = False
                    lnkLastPage.Enabled = False
                End If
            Else
                lnkPreviousPage.Enabled = True

                If (intPageNumber = dblPagesAffected) Then
                    lnkNextPage.Enabled = False
                    lnkLastPage.Enabled = False
                Else
                    lnkNextPage.Enabled = True
                    lnkLastPage.Enabled = True
                End If
            End If

            If (DotNetNuke.Security.PortalSecurity.IsInRoles(Me.ModuleConfiguration.AuthorizedEditRoles)) And Not (UserId = -1) Then
                pnlEditPanel.Visible = True
                grdMobs.Columns(0).Visible = True
            Else
                pnlEditPanel.Visible = False
                grdMobs.Columns(0).Visible = False
            End If

        End Sub

        Private Sub ManagePageState(ByVal PageState As PageState)

            Dim tss_DKP As New tss_DataController

            vsPageState = PageState

            If (PageState = PageState.View) Then
                grdMobs.Enabled = True
                BindPage()
            Else
                grdMobs.Enabled = False
            End If

            If (PageState = PageState.View) Then
                pnlEditPanelForm.Visible = False
            Else

                ddlInstanceID.DataTextField = "InstanceName"
                ddlInstanceID.DataValueField = "InstanceID"
                ddlInstanceID.DataSource = tss_DKP.tss_GetInstances(CInt(Settings("BoardID")))
                ddlInstanceID.DataBind()

                pnlEditPanelForm.Visible = True

            End If

            txtMobName.Text = Null.NullString
            txtMobValue.Text = Null.NullString
            txtMobWowHeadID.Text = Null.NullString
            ddlInstanceID.ClearSelection()
            chkMobHeroicFlag.Checked = False
            chkMobAttendanceFlag.Checked = False

            lblWarning.Text = Null.NullString

            If (PageState = PageState.Add) Or (PageState = PageState.SingleUpdate) Then

                pnlMobName.Visible = True
                pnlMobWowHeadID.Visible = True

                chkIncludeMobName.Visible = False
                chkIncludeMobValue.Visible = False
                chkIncludeMobWowHeadID.Visible = False
                chkIncludeMobHeroicFlag.Visible = False
                chkIncludeMobAttendanceFlag.Visible = False
                chkIncludeInstanceID.Visible = False

                txtMobName.Focus()

            ElseIf (PageState = PageState.MultiUpdate) Then

                pnlMobName.Visible = False
                pnlMobWowHeadID.Visible = False

                chkIncludeMobName.Visible = False
                chkIncludeMobValue.Visible = True
                chkIncludeMobWowHeadID.Visible = False
                chkIncludeMobHeroicFlag.Visible = True
                chkIncludeMobAttendanceFlag.Visible = True
                chkIncludeInstanceID.Visible = True

                chkIncludeMobValue.Checked = False
                chkIncludeMobWowHeadID.Checked = False
                chkIncludeMobHeroicFlag.Checked = False
                chkIncludeMobAttendanceFlag.Checked = False
                chkIncludeInstanceID.Checked = False

                txtMobValue.Focus()

            End If

        End Sub

        Private Function SyncMob(ByVal intMobID As Integer) As String

            Dim rgxItemMatchCollectionsL0, rgxItemMatchCollectionsL1 As MatchCollection

            Dim strItemNormalRegexPatterns(1) As String
            strItemNormalRegexPatterns(0) = "^new Listview\(\{template: 'item', id: 'normal-drops', [a-zA-Z0-9:.,'_\\\[\]\{\}\-\(\) ]*\);"
            strItemNormalRegexPatterns(1) = "id:([0-9]*),name:'([a-zA-Z0-9\\' ]*)'"

            Dim strItemHeroicRegexPatterns(1) As String
            strItemHeroicRegexPatterns(0) = "^new Listview\(\{template: 'item', id: 'heroic-drops', [a-zA-Z0-9:.,'_\\\[\]\{\}\-\(\) ]*\);"
            strItemHeroicRegexPatterns(1) = "id:([0-9]*),name:'([a-zA-Z0-9\\' ]*)'"

            Dim objWebClient As New WebClient()

            Dim strMobURL As String
            Dim objMobUTF8 As New UTF8Encoding()

            Dim tss_DKP As New tss_DataController
            Dim objMob As tss_MobInfo
            Dim objItem As New tss_ItemInfo
            Dim objDrop As New tss_DropInfo
            Dim objExistingItem As tss_ItemInfo

            objItem = CBO.InitializeObject(objItem, GetType(tss_ItemInfo))
            objDrop = CBO.InitializeObject(objDrop, GetType(tss_DropInfo))

            objMob = tss_DKP.tss_GetMob(intMobID)

            If Not objMob Is Nothing Then

                If (objMob.MobWowHeadID = "") Then
                    Return Null.NullString
                End If

                strMobURL = "http://www.wowhead.com/?npc=" + objMob.MobWowHeadID

                If Not (objMob.MobHeroicFlag) Then

                    rgxItemMatchCollectionsL0 = Regex.Matches(objMobUTF8.GetString(objWebClient.DownloadData(strMobURL)), _
                                  strItemNormalRegexPatterns(0), RegexOptions.Multiline)

                    For Each rgxItemMatchL0 In rgxItemMatchCollectionsL0

                        rgxItemMatchCollectionsL1 = Regex.Matches(rgxItemMatchL0.Value, strItemNormalRegexPatterns(1))

                        For Each rgxItemMatchL1 In rgxItemMatchCollectionsL1

                            objExistingItem = tss_DKP.tss_GetItemByName(rgxItemMatchL1.Groups.Item(2).Value.Replace("\", "").Remove(0, 1), CInt(Settings("BoardID")))

                            If (objExistingItem Is Nothing) Then

                                objItem.ItemName = rgxItemMatchL1.Groups.Item(2).Value.Replace("\", "").Remove(0, 1)
                                objItem.ItemValue = 0
                                objItem.ItemWowHeadID = rgxItemMatchL1.Groups.Item(1).Value
                                objItem.BoardID = CInt(Settings("BoardID"))

                                Try
                                    objItem.ItemID = tss_DKP.tss_AddItem(objItem)
                                Catch ex As ApplicationException
                                End Try

                            Else

                                objItem = objExistingItem

                            End If

                            objDrop.MobID = objMob.MobID
                            objDrop.ItemID = objItem.ItemID

                            Try
                                tss_DKP.tss_AddDrop(objDrop)
                            Catch ex As ApplicationException
                            End Try

                        Next
                    Next

                ElseIf (objMob.MobHeroicFlag) Then

                    rgxItemMatchCollectionsL0 = Regex.Matches(objMobUTF8.GetString(objWebClient.DownloadData(strMobURL)), _
                                                                    strItemHeroicRegexPatterns(0), RegexOptions.Multiline)

                    For Each rgxItemMatchL0 In rgxItemMatchCollectionsL0

                        rgxItemMatchCollectionsL1 = Regex.Matches(rgxItemMatchL0.Value, strItemNormalRegexPatterns(1))

                        For Each rgxItemMatchL1 In rgxItemMatchCollectionsL1

                            objExistingItem = tss_DKP.tss_GetItemByName(rgxItemMatchL1.Groups.Item(2).Value.Replace("\", "").Remove(0, 1), CInt(Settings("BoardID")))

                            If (objExistingItem Is Nothing) Then

                                objItem.ItemName = rgxItemMatchL1.Groups.Item(2).Value.Replace("\", "").Remove(0, 1)
                                objItem.ItemValue = 0
                                objItem.ItemWowHeadID = rgxItemMatchL1.Groups.Item(1).Value
                                objItem.BoardID = CInt(Settings("BoardID"))

                                Try
                                    objItem.ItemID = tss_DKP.tss_AddItem(objItem)
                                Catch ex As ApplicationException
                                End Try

                            Else

                                objItem = objExistingItem

                            End If

                            objDrop.MobID = objMob.MobID
                            objDrop.ItemID = objItem.ItemID

                            Try
                                tss_DKP.tss_AddDrop(objDrop)
                            Catch ex As ApplicationException
                            End Try

                        Next

                    Next

                End If

            End If

            Return Null.NullString

        End Function

#End Region

#Region "Private Event Handlers"

        Private Sub ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grdMobs.ItemDataBound

            If (e.Item.ItemType.Equals(ListItemType.Item)) Or (e.Item.ItemType.Equals(ListItemType.AlternatingItem)) Then

                Dim lblMobID As Label = e.Item.FindControl("lblMobID")
                Dim btnDrops As ImageButton = e.Item.FindControl("btnDrops")
                Dim lnkMobWowHeadID As HyperLink = e.Item.FindControl("lnkMobWowHeadID")
                Dim lblMobName As Label = e.Item.FindControl("lblMobName")
                Dim lblMobValue As Label = e.Item.FindControl("lblMobValue")
                Dim lblInstanceName As Label = e.Item.FindControl("lblInstanceName")
                Dim imgMobHeroicFlag As Image = e.Item.FindControl("imgMobHeroicFlag")
                Dim imgMobAttendanceFlag As Image = e.Item.FindControl("imgMobAttendanceFlag")

                Dim objMob As tss_MobInfo = e.Item.DataItem

                lblMobID.Text = objMob.MobID.ToString()
                lblMobID.Visible = False

                btnDrops.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_bags.png"
                btnDrops.CommandName = "Drops"
                btnDrops.CommandArgument = objMob.MobID.ToString()
                btnDrops.AlternateText = "View Drops"

                If (objMob.MobWowHeadID.Length = 0) Then
                    lnkMobWowHeadID.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_blank.png"
                    lnkMobWowHeadID.Enabled = False
                Else
                    lnkMobWowHeadID.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_wowhead.png"
                    lnkMobWowHeadID.NavigateUrl = "http://www.wowhead.com/?npc=" + objMob.MobWowHeadID
                    lnkMobWowHeadID.Target = "_new"
                    lnkMobWowHeadID.Enabled = True
                    lnkMobWowHeadID.ToolTip = "View WowHead"
                End If

                lblMobName.Text = objMob.MobName
                lblMobValue.Text = objMob.MobValue
                lblInstanceName.Text = objMob.InstanceName

                If (objMob.MobHeroicFlag) Then
                    imgMobHeroicFlag.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_checked.png"
                Else
                    imgMobHeroicFlag.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_blank.png"
                End If

                If (objMob.MobAttendanceFlag) Then
                    imgMobAttendanceFlag.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_checked.png"
                Else
                    imgMobAttendanceFlag.ImageUrl = "/DesktopModules/tss_DKP/Images/grid_icon_blank.png"
                End If

            End If

        End Sub

        Private Sub ItemCommand(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles grdMobs.ItemCommand

            Dim tss_DKP As New tss_DataController
            Dim tss_DKP_Parent As tss_DKP = Me.Parent.Parent

            If (e.CommandName = "Drops") Then

                Dim objMob As tss_MobInfo = tss_DKP.tss_GetMob(CInt(e.CommandArgument))
                If (objMob Is Nothing) Then
                    ManagePageState(PageState.View)
                Else
                    tss_DKP_Parent.lnkManageMobDrops_Click(objMob)
                End If

            End If

        End Sub

        Private Sub ItemCreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles grdMobs.ItemCreated

            If (e.Item.ItemType.Equals(ListItemType.Header)) Then

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridHeader" + grdMobs.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.Item)) Then

                e.Item.Attributes.Add("class", "dkpdnn_GridItem")
                e.Item.Attributes.Add("onmouseover", "className='dkpdnn_GridItemMouseOver';")
                e.Item.Attributes.Add("onmouseout", "className='dkpdnn_GridItem';")

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridItem" + grdMobs.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.AlternatingItem)) Then

                e.Item.Attributes.Add("class", "dkpdnn_GridAltItem")
                e.Item.Attributes.Add("onmouseover", "className='dkpdnn_GridAltItemMouseOver';")
                e.Item.Attributes.Add("onmouseout", "className='dkpdnn_GridAltItem';")

                For Each currentCell As TableCell In e.Item.Cells
                    currentCell.Attributes.Add("class", "dkpdnn_GridAltItem" + grdMobs.Columns(e.Item.Cells.GetCellIndex(currentCell)).FooterText)
                Next

            ElseIf (e.Item.ItemType.Equals(ListItemType.Footer)) Then

                e.Item.Visible = False

            End If

        End Sub

        Private Sub SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlPageSize.SelectedIndexChanged

            If (sender.Equals(ddlPageSize)) Then

                vsPageSize = ddlPageSize.SelectedValue
                ManagePageState(PageState.View)

            End If

        End Sub

        Private Sub SortCommand(ByVal sender As Object, ByVal e As DataGridSortCommandEventArgs) Handles grdMobs.SortCommand

            If (vsSortExpression = e.SortExpression) Then
                If (vsSortDirection = "ASC") Then
                    vsSortDirection = "DESC"
                ElseIf (vsSortDirection = "DESC") Then
                    vsSortDirection = "ASC"
                End If
            Else
                vsSortExpression = e.SortExpression
                vsSortDirection = "ASC"
            End If

            ManagePageState(PageState.View)

        End Sub

        Private Sub Click(ByVal sender As Object, ByVal e As EventArgs) Handles lnkFirstPage.Click, lnkPreviousPage.Click, _
            lnkNextPage.Click, lnkLastPage.Click, lnkAdd.Click, lnkUpdate.Click, lnkDelete.Click, lnkSync.Click, lnkSubmit.Click, lnkCancel.Click

            Dim tss_DKP As New tss_DataController

            lblWarning.Text = Null.NullString

            If (sender.Equals(lnkFirstPage)) Then

                vsCurrentPage = 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkPreviousPage)) Then

                vsCurrentPage = CInt(vsCurrentPage) - 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkNextPage)) Then

                vsCurrentPage = CInt(vsCurrentPage) + 1
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkLastPage)) Then

                vsCurrentPage = vsTotalPages
                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkAdd)) Then

                ManagePageState(PageState.Add)

            ElseIf (sender.Equals(lnkUpdate)) Then

                Dim boolIsChecked As Boolean = False

                For Each currentMob As DataGridItem In grdMobs.Items

                    If (currentMob.ItemType.Equals(ListItemType.Item)) Or (currentMob.ItemType.Equals(ListItemType.AlternatingItem)) Then

                        Dim chkMob As CheckBox = currentMob.FindControl("chkMob")
                        Dim lblMobID As Label = currentMob.FindControl("lblMobID")

                        If (chkMob.Checked) And (boolIsChecked) Then

                            ManagePageState(PageState.MultiUpdate)
                            Exit For

                        ElseIf (chkMob.Checked) And Not (boolIsChecked) Then

                            boolIsChecked = True

                            ManagePageState(PageState.SingleUpdate)

                            Dim objExistingMob As tss_MobInfo = tss_DKP.tss_GetMob(CInt(lblMobID.Text))

                            If (objExistingMob Is Nothing) Then
                                ManagePageState(PageState.View)
                                lblWarning.Text = "Error: Mob in selection not found."
                                Exit For
                            Else
                                txtMobName.Text = objExistingMob.MobName
                                txtMobValue.Text = objExistingMob.MobValue
                                txtMobWowHeadID.Text = objExistingMob.MobWowHeadID
                                chkMobHeroicFlag.Checked = objExistingMob.MobHeroicFlag
                                chkMobAttendanceFlag.Checked = objExistingMob.MobAttendanceFlag
                                ddlInstanceID.SelectedValue = objExistingMob.InstanceID
                            End If

                        End If

                    End If

                Next

                If Not (boolIsChecked) Then
                    ManagePageState(PageState.View)
                End If

            ElseIf (sender.Equals(lnkDelete)) Then

                For Each currentMob As DataGridItem In grdMobs.Items

                    If (currentMob.ItemType.Equals(ListItemType.Item)) Or (currentMob.ItemType.Equals(ListItemType.AlternatingItem)) Then

                        Dim chkMob As CheckBox = currentMob.FindControl("chkMob")
                        Dim lblMobID As Label = currentMob.FindControl("lblMobID")

                        If (chkMob.Checked) Then
                            tss_DKP.tss_DeleteMob(CInt(lblMobID.Text))
                        End If

                    End If

                Next

                ManagePageState(PageState.View)

            ElseIf (sender.Equals(lnkSync)) Then

                Dim strWarning As String = Null.NullString

                For Each currentMob As DataGridItem In grdMobs.Items

                    If (currentMob.ItemType.Equals(ListItemType.Item)) Or (currentMob.ItemType.Equals(ListItemType.AlternatingItem)) Then

                        Dim chkMob As CheckBox = currentMob.FindControl("chkMob")
                        Dim lblMobID As Label = currentMob.FindControl("lblMobID")

                        If (chkMob.Checked) Then
                            strWarning = strWarning + IIf(strWarning.Length > 0, "<br>", "") + SyncMob(CInt(lblMobID.Text))
                        End If

                    End If

                Next

                ManagePageState(PageState.View)
                lblWarning.Text = strWarning

            ElseIf (sender.Equals(lnkSubmit)) Then

                If (vsPageState = PageState.Add) Then

                    If (txtMobName.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Mob name can not be empty."
                    ElseIf (txtMobValue.Text.Trim.Length = 0) Then
                        lblWarning.Text = "Error: Mob value can not be empty."
                    ElseIf (ddlInstanceID.SelectedValue.Length = 0) Then
                        lblWarning.Text = "Error: Instance name can not be empty."
                    Else

                        Dim objNewMob As New tss_MobInfo : objNewMob = CBO.InitializeObject(objNewMob, GetType(tss_MobInfo))

                        objNewMob.MobName = txtMobName.Text.Trim
                        objNewMob.MobValue = txtMobValue.Text.Trim
                        objNewMob.MobWowHeadID = txtMobWowHeadID.Text.Trim
                        objNewMob.MobHeroicFlag = chkMobHeroicFlag.Checked
                        objNewMob.MobAttendanceFlag = chkMobAttendanceFlag.Checked
                        objNewMob.InstanceID = ddlInstanceID.SelectedValue

                        Try
                            tss_DKP.tss_AddMob(objNewMob)
                        Catch ex As ApplicationException
                            lblWarning.Text = "Error: Trying to add mob resulted in the following error (" + ex.Message + ")."
                        End Try

                    End If

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                ElseIf (vsPageState = PageState.SingleUpdate) Then

                    For Each currentMob As DataGridItem In grdMobs.Items

                        If (currentMob.ItemType.Equals(ListItemType.Item)) Or (currentMob.ItemType.Equals(ListItemType.AlternatingItem)) Then

                            Dim chkMob As CheckBox = currentMob.FindControl("chkMob")
                            Dim lblMobID As Label = currentMob.FindControl("lblMobID")

                            If (chkMob.Checked) Then

                                Dim objExistingMob As tss_MobInfo = tss_DKP.tss_GetMob(CInt(lblMobID.Text))

                                If (objExistingMob Is Nothing) Then
                                    lblWarning.Text = "Error: Trying to update mob resulted in the following error (Mob in selection not found.)."
                                Else

                                    objExistingMob.MobName = txtMobName.Text.Trim
                                    objExistingMob.MobValue = txtMobValue.Text.Trim
                                    objExistingMob.MobWowHeadID = txtMobWowHeadID.Text.Trim
                                    objExistingMob.MobHeroicFlag = chkMobHeroicFlag.Checked
                                    objExistingMob.MobAttendanceFlag = chkMobAttendanceFlag.Checked
                                    objExistingMob.InstanceID = ddlInstanceID.SelectedValue

                                    Try
                                        tss_DKP.tss_UpdateMob(objExistingMob)
                                    Catch ex As ApplicationException
                                        lblWarning.Text = "Error: Trying to update mob resulted in the following error (" + ex.Message + ")."
                                    End Try

                                End If

                            End If

                        End If

                    Next

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                ElseIf (vsPageState = PageState.MultiUpdate) Then

                    For Each currentMob As DataGridItem In grdMobs.Items

                        If (currentMob.ItemType.Equals(ListItemType.Item)) Or (currentMob.ItemType.Equals(ListItemType.AlternatingItem)) Then

                            Dim chkMob As CheckBox = currentMob.FindControl("chkMob")
                            Dim lblMobID As Label = currentMob.FindControl("lblMobID")

                            If (chkMob.Checked) Then

                                Dim objExistingMob As tss_MobInfo = tss_DKP.tss_GetMob(CInt(lblMobID.Text))

                                If (objExistingMob Is Nothing) Then
                                    lblWarning.Text = lblWarning.Text + IIf(lblWarning.Text.Length > 0, "<br>", "") _
                                        + "Error: Trying to update mob resulted in the following error (Mob in selection not found.)."
                                Else

                                    If (chkIncludeMobValue.Checked) Then
                                        objExistingMob.MobValue = txtMobValue.Text.Trim
                                    End If

                                    If (chkIncludeMobHeroicFlag.Checked) Then
                                        objExistingMob.MobHeroicFlag = chkMobHeroicFlag.Checked
                                    End If

                                    If (chkIncludeMobAttendanceFlag.Checked) Then
                                        objExistingMob.MobAttendanceFlag = chkMobAttendanceFlag.Checked
                                    End If

                                    If (chkIncludeInstanceID.Checked) Then
                                        objExistingMob.InstanceID = ddlInstanceID.SelectedValue
                                    End If

                                    Try
                                        tss_DKP.tss_UpdateMob(objExistingMob)
                                    Catch ex As ApplicationException
                                        lblWarning.Text = lblWarning.Text + IIf(lblWarning.Text.Length > 0, "<br>", "") _
                                            + "Error: Trying to update mob (" + objExistingMob.MobName _
                                            + ") resulted in the following error (" + ex.Message + ")."
                                    End Try

                                End If

                            End If

                        End If

                    Next

                    If (lblWarning.Text = Null.NullString) Then
                        ManagePageState(PageState.View)
                    End If

                End If

            ElseIf (sender.Equals(lnkCancel)) Then

                ManagePageState(PageState.View)

            End If

        End Sub

#End Region

#Region "Public Methods"

        Public Sub Initialize()

            ManagePageState(PageState.View)

        End Sub

#End Region

    End Class

End Namespace
